package study.saas.web.portal.sys.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Commodity;
import study.saas.order.service.CommodityFacade;
import study.saas.order.service.model.CommodityFiterDTO;
import study.saas.order.service.model.CommodityModifyDTO;

/**
 * @author ctc
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping(value = "sys/commodity")
public class CommodityController {

    @Reference(version = "1.0.0")
    private CommodityFacade commodityFacade;

    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResultV2 create(CommodityModifyDTO commodity) {
        if (commodity == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.addCommodity(commodity);
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ApiResultV2 modify(CommodityModifyDTO commodity) {
        return commodityFacade.setCommodity(commodity);
    }

    @ApiOperation(value = "查询")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ApiResultV2<Commodity> get(String name) {
        Commodity commodity = new Commodity();
        if (name==null){
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.findCommodity(name);
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResultV2 list(CommodityFiterDTO commodity) {
        if (commodity == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.list(commodity);
    }

    /**
     * 上下架
     * @param commodityName
     * @param isState 0 上架  1 下架
     * @return
     */
    @ApiOperation(value = "上架/下架")
    @RequestMapping(value = "/setCommodityState", method = RequestMethod.POST)
    public ApiResultV2 setCommodityState(String commodityName,Integer isState) {
        if (commodityName == null||isState == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        Commodity commodity = commodityFacade.findCommodity(commodityName).getData();
        if (commodity==null||commodity.isDelete()){
            return ApiResultV2.error("商品不存在！");
        }
        CommodityModifyDTO commodity1 = new CommodityModifyDTO();
        CopyClassUtil.copyClass(commodity, commodity1);
        commodity1.setCommodityState(isState);
        return commodityFacade.setCommodity(commodity1);
    }

    /**
     * 库存变动
     * @param flag true 增加  flag减少
     * @param num
     * @return
     */
    @ApiOperation(value = "库存变动")
    @RequestMapping(value = "/setAmount", method = RequestMethod.POST)
    public ApiResultV2 setAmount(Boolean flag, String commodityName, Integer num) {
        if (num == null||commodityName == null) {
            return ApiResultV2.error("数据不能为空！");
        }
//        Commodity commodity = commodityFacade.findCommodity(commodityName).getData();
//        if (commodity==null||commodity.isDelete()){
//            return ApiResultV2.error("商品不存在！");
//        }
//        CommodityModifyDTO commodity1 = new CommodityModifyDTO();
//        CopyClassUtil.copyClass(commodity, commodity1);
//        if (flag){
//            commodity1.setAmount(commodity1.getAmount()+num);
//        }else {
//            commodity1.setAmount(commodity1.getAmount()-num);
//        }
//        return commodityFacade.setCommodity(commodity1);
        return commodityFacade.setAmount(flag,commodityName,num);
    }



    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultV2 delete(String name) {
        if (name == null) {
            return ApiResultV2.error("数据不能为空！");
        }
        return commodityFacade.deleteCommodity(name);
    }

}
